package com.ziq.zcamp.view.edit;

import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.List;

public class SupportTopicsTextHelp {

    /**
     * 返回前15个符合话题的列表里面的话题
     * @param position 用于支持列表中的话题
     * @param content 内容
     * @param topics 话题列表
     * @param topicsOfIndex  HashMap 在列表中使用的话传递此属性用于判断是否总的超过了15
     * @return 位置列表
     */
    public static HashMap<TopicsKeyModel,String> help(@Nullable String content, @Nullable List<String> topics ,
                                                      @Nullable Integer position ,@Nullable HashMap<TopicsKeyModel,String> topicsOfIndex ){
        if (topics == null || topics.size() == 0)return new HashMap<>(0);
        if (content == null || TextUtils.isEmpty(content))return new HashMap<>(0);
        if (topicsOfIndex == null){
            topicsOfIndex = new HashMap<>(15);
        }
        if (position == null){
            position = -1;
        }

        int findSymbol = -1;
        for (int i = 0; i < content.length(); i++) {
            int current = Character.codePointAt(content, i);
            if (current == '#') {
                findSymbol = i;
            }

            if (findSymbol != -1 && i > findSymbol) {
                //当他是最后一位的时候也判断也一下，不然不显示
                if (current == ' ' || i == content.length()-1) {
                    //截断当前话题,并记录
                    if (topicsOfIndex.size()>=15){
                        break;
                    }else {
                        int endIndex = (current != ' ' && i==content.length()-1)?content.length():i;
                        String con = content.substring(findSymbol +1,endIndex);
                        if (topics.contains(con)){
                            TopicsKeyModel model = new TopicsKeyModel(position,findSymbol);
                            topicsOfIndex.put(model,content.substring(findSymbol,endIndex));
                        }
                    }
                    findSymbol = -1;
                }
            }
        }
        return topicsOfIndex;
    }
}
